
const {RoleModel,MenuModel,RoleMenuModel} = require('../../models/index')
const koaTool = require('../../commons/koa_tool')


class RoleController {

    //新增
    create = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['name','disable'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let dataCount = await RoleModel.countDocuments({name: input.name});
        if(dataCount > 0){
            return koaTool.error(ctx, "该名称已存在");
        }

        let newData = Object.assign({}, input, {
            
        })

        let detail = await RoleModel.create(newData)
        return koaTool.success(ctx,'创建成功');
    }

    //详情
    detail = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        const detail = await RoleModel.findById(input.id)
        if(detail){
            return koaTool.success(ctx,'获取成功',{
                detail
            });
        }
        return koaTool.error(ctx,'无该菜单信息');
    }
    //更新
    update = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let detail = await RoleModel.findById(input.id)
        if (!detail) {
            return koaTool.succ(ctx, {}, 405, '数据不存在')
        }
        const {
            name , disable
        } = input
        if (name) {
            detail.name = name
        }
        if (disable) {
            detail.disable = disable
        }
        await detail.save()
        return koaTool.success(ctx, '编辑成功');
    }
    //删除
    delete = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        await RoleModel.findByIdAndRemove(input.id)
        return koaTool.success(ctx, '删除成功');
    }
    //列表
    list = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let query = {}
        let list = await RoleModel.find(query).sort([['create_at', -1]]);
        koaTool.succ(ctx, {
            list
        })
    }
    getValidList = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let query = {
            'disable':1
        }
        let list = await RoleModel.find(query).sort([['create_at', -1]]);
        koaTool.succ(ctx, {
            list
        });
    }
    getRoleMenuList = async (ctx) => {
        
        let [checkError, input] = koaTool.checkInput(ctx, ['role'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let {role} = input;
        let roleInfo = await RoleModel.findById(role);
        if(!roleInfo){
            return koaTool.error(ctx,'角色异常');
        }

        let query = {
            'disable':1
        }
        let menuList = await MenuModel.find(query).sort([['order', 1]]);
        let roleMenuQuery = {
            role
        };
        let roleMenuList = await RoleMenuModel.find(roleMenuQuery);
        let menuIdList = [];
        roleMenuList.map( (v) =>{
            menuIdList.push(v.menu.toString());
        });

        console.log('menuIdList:',menuIdList);
        let list = [];
        menuList.map((v) => {
            let item = {
                _id:v._id,
                name:v.name,
                code:v.code,
                parent_code:v.parent_code,
                select:menuIdList.indexOf(v._id.toString()) != -1 ? 1 :0
            }
            list.push(item);
        })

        koaTool.succ(ctx, {
            list
        });
    }
    setRoleMenu = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['role','menuIdList'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let {role,menuIdList} = input;
        let roleInfo = await RoleModel.findById(role);
        if(!roleInfo){
            return koaTool.error(ctx,'角色异常');
        }
        let deleteResult = await RoleMenuModel.deleteMany({role});

        menuIdList = JSON.parse(menuIdList);
        for(var i = 0, l = menuIdList.length; i < l; i++){
            let createResult = await RoleMenuModel.create({role,menu:menuIdList[i]});
        }
        return koaTool.success(ctx,'编辑成功');
    }
}

module.exports=new RoleController()